import { Scene } from '@babylonjs/core/scene';
import { AbstractControl, IAbstractControlOptions } from './abstract-control';
import { Control } from '@babylonjs/gui/2D/controls/control';
import { TextBlock, TextWrapping } from '@babylonjs/gui/2D/controls/textBlock';


/**
 * 文字块控件
 * @param text 内容
 * @param fontColor 字体颜色
 * @param fontSize 字体大小
 * @param fontFamily 字体
 * @param fontStyle 字体样式
 * @param fontWeight 字体权重
 * @param lineSpacing 行间距
 * @param lineThrough 是否划线（文字划线）
 * @param textHorizontalAlignment 内容水平布局
 * @param textVerticalAlignment 内容垂直布局
 * @param textWrapping 如何断行
 * @param underline 是否下划线
 */
interface IUITextOptions extends IAbstractControlOptions {
    text?: string,
    fontColor?: string,
    fontSize?: number,
    fontFamily?: string,
    fontStyle?: string,
    fontWeight?: string,
    lineSpacing?: number,
    lineThrough?: boolean,
    textHorizontalAlignment?: number,
    textVerticalAlignment?: number,
    textWrapping?: boolean | TextWrapping,
    underline?: boolean,
}


/**
 * 文字块控件
 * @param name 名称
 * @param scene 所在场景
 * @param options 参数对象
 */
class UIText extends AbstractControl {

    constructor(name: string, scene: Scene, options?: IUITextOptions) {
        super(name, scene, options);
    }

    /**
     * 设置内容
     */
    public set text(text: string) {
        (this.control as TextBlock).text = text;
    }

    /**
     * 获取内容
     */
    public get text(): string {
        return (this.control as TextBlock).text;
    }

    /**
     * 设置字体颜色
     */
    public set fontColor(color: string) {
        (this.control as TextBlock).color = color;
    }

    /**
     * 获取字体颜色
     */
    public get fontColor(): string {
        return (this.control as TextBlock).color;
    }

    public set fontSize(value: number) {
        (this.control as TextBlock).fontSize = value;
    }

    public get fontSize(): number {
        return (this.control as TextBlock).fontSize as number;
    }

    protected initializeControl(name: string, scene: Scene, options: IUITextOptions): Control {
        const text = new TextBlock(name + '-control', options.text || 'text');
        text.color = options.fontColor || '#000000';
        text.fontSize = options.fontSize || 26;
        text.fontFamily = options.fontFamily || 'Arial';
        text.fontStyle = options.fontStyle || 'normal';
        text.fontWeight = options.fontWeight || 'normal';
        text.lineSpacing = options.lineSpacing || 5;
        text.lineThrough = options.lineThrough || false;
        text.textHorizontalAlignment = options.textHorizontalAlignment === undefined ? Control.HORIZONTAL_ALIGNMENT_CENTER : options.textHorizontalAlignment;
        text.textVerticalAlignment = options.textVerticalAlignment === undefined ? Control.VERTICAL_ALIGNMENT_CENTER : options.textVerticalAlignment;
        text.textWrapping = options.textWrapping === undefined ? 4 : options.textWrapping;
        text.underline = options.underline || false;

        return text;
    }

    protected bindEvent(control: Control, options: IAbstractControlOptions): void {
    }

}


/**
 * 创建文字块控件
 * @param name 名称
 * @param scene 所在场景
 * @param options 参数对象
 * @returns 文字块控件
 */
const CreateUIText = (name: string, scene: Scene, options?: IUITextOptions) => {
    return new UIText(name, scene, options);
};


export { UIText, CreateUIText };
export type { IUITextOptions };